package com.max;

import com.max.dao.UserinfoDao;
import com.max.entity.Userinfo;

import java.util.List;

public class Test {

    public static void main(String[] args) {

        // BaseDao<Userinfo> baseDao = new BaseDao<>();
        /*List<Userinfo> userinfoList = baseDao.queryExecute(
                Userinfo.class,
                "select * from userinfo where id = ?",
                "2 or 1=1"
        );*/

        //  args[0] = 1;  => select * from userinfo where id = 1;
        //  args[0] = 1 or 1=1; =>  select * from userinfo where id = 1 or 1=1; => SQL注入攻击

        // select * from userinfo where id = '1 or 1=1'
        // => 能查出数据是因为=后面的字符串会发生类型转换，
        // 转换规则是直接转换为从第一个字符开始到第一个出现非数字字符结束的内容这部非内容直接转换为数字

//        System.out.println(userinfoList);

        /*int i = baseDao.actionExecute(
                "insert into userinfo(username, login_name, head_img) values(?,?,?)",
                "max001",
                "max001-login",
                "max.jpg"
        );

        System.out.println("执行结果：" + i);

        List<Userinfo> userinfoList = baseDao.queryExecute(
                Userinfo.class,
                "select * from userinfo"
        );

        System.out.println(userinfoList);*/


        UserinfoDao userinfoDao = UserinfoDao.getInstance();


        int i = userinfoDao.deleteById(2L);
        System.out.println("删除执行结果：" + i);


        Userinfo userinfo = new Userinfo();
        userinfo.setUsername("max");
//        userinfo.setLoginName("max-login");
        List<Userinfo> userinfoList = userinfoDao.selectList(userinfo);

        System.out.println(userinfoList);


        Userinfo userinfoInsert = new Userinfo();
        userinfoInsert.setUsername("new-user-01");
        userinfoInsert.setLoginName("user-login-name");
        userinfoInsert.setPwd("<PASSWORD>");
        int insert = userinfoDao.insert(userinfoInsert);
        System.out.println("插入结果：" + insert);

        Userinfo userinfoUpdate = new Userinfo();
        userinfoUpdate.setId(5L);
        userinfoUpdate.setUsername("max-update");
        int update = userinfoDao.updateById(userinfoUpdate);

        System.out.println("更新结果：" + update);

        List<Userinfo> userinfoListUpdate = userinfoDao.selectList(userinfoUpdate);
        System.out.println(userinfoListUpdate);

    }


}
